/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PublicHolidayEditor.java
 *
 * Created on 14-Apr-2011, 23:00:45
 */

package TimeRecording.forms;

import TimeRecording.tables.PublicHoliday;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author david
 */
public class PublicHolidayEditor extends javax.swing.JDialog {

    // <editor-fold defaultstate="collapsed" desc="publicHoliday + Getter & Setter">
    private PublicHoliday publicHoliday;

    public PublicHoliday getPublicHoliday() {
        return publicHoliday;
    }

    public void setPublicHoliday(PublicHoliday publicHoliday) {
        PublicHoliday oldPublicHoliday = this.publicHoliday;
        this.publicHoliday = publicHoliday;
        firePropertyChange("publicHoliday", oldPublicHoliday, publicHoliday);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="saved + Getter & Setter">
    private boolean saved;

    public boolean getSaved() {
        return saved;
    }

    public void setSaved(boolean saved) {
        boolean oldSaved = this.saved;
        this.saved = saved;
        firePropertyChange("saved", oldSaved, saved);
    } // </editor-fold>

    /** Creates new form PublicHolidayEditor */
    public PublicHolidayEditor(PublicHolidayDialog parent) {
        super(parent, true);
        myParent = parent;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        holidayIdLabel = new javax.swing.JLabel();
        holidayId = new javax.swing.JTextField();
        dateOfHolidayLabel = new javax.swing.JLabel();
        dateOfHoliday = new com.toedter.calendar.JDateChooser();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        descriptionLabel = new javax.swing.JLabel();
        description = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(PublicHolidayEditor.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        holidayIdLabel.setText(resourceMap.getString("holidayIdLabel.text")); // NOI18N
        holidayIdLabel.setName("holidayIdLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 4);
        getContentPane().add(holidayIdLabel, gridBagConstraints);

        holidayId.setEditable(false);
        holidayId.setMaximumSize(new java.awt.Dimension(100, 2147483647));
        holidayId.setMinimumSize(new java.awt.Dimension(100, 20));
        holidayId.setName("holidayId"); // NOI18N
        holidayId.setPreferredSize(new java.awt.Dimension(100, 20));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${publicHoliday.holidayId}"), holidayId, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 8);
        getContentPane().add(holidayId, gridBagConstraints);

        dateOfHolidayLabel.setText(resourceMap.getString("dateOfHolidayLabel.text")); // NOI18N
        dateOfHolidayLabel.setName("dateOfHolidayLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        getContentPane().add(dateOfHolidayLabel, gridBagConstraints);

        dateOfHoliday.setMinimumSize(new java.awt.Dimension(150, 20));
        dateOfHoliday.setName("dateOfHoliday"); // NOI18N
        dateOfHoliday.setPreferredSize(new java.awt.Dimension(80, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${publicHoliday.dateOfHoliday}"), dateOfHoliday, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 8);
        getContentPane().add(dateOfHoliday, gridBagConstraints);

        buttonPanel.setName("buttonPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getActionMap(PublicHolidayEditor.class, this);
        okButton.setAction(actionMap.get("okClicked")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        buttonPanel.add(okButton);

        cancelButton.setAction(actionMap.get("cancelClicked")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        buttonPanel.add(cancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(buttonPanel, gridBagConstraints);

        descriptionLabel.setText(resourceMap.getString("descriptionLabel.text")); // NOI18N
        descriptionLabel.setName("descriptionLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        getContentPane().add(descriptionLabel, gridBagConstraints);

        description.setName("description"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${publicHoliday.description}"), description, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 8);
        getContentPane().add(description, gridBagConstraints);

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-168)/2, 416, 168);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void okClicked() {

        if (!validatePublicHoliday()) {
            return;
        }

        setSaved(true);
        setVisible(false);
    }

    @Action
    public void cancelClicked() {
        setSaved(false);
        setVisible(false);
    }

    private boolean validatePublicHoliday() {
        
        if (publicHoliday.getDateOfHoliday() == null ||
                publicHoliday.getDescription() == null ||
                publicHoliday.getDescription().length() == 0) {

            JOptionPane.showMessageDialog(this,
                    "The holiday must have a name and a date",
                    "Save Public Holiday",
                    JOptionPane.ERROR_MESSAGE);

            return false;
        }

        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private com.toedter.calendar.JDateChooser dateOfHoliday;
    private javax.swing.JLabel dateOfHolidayLabel;
    private javax.swing.JTextField description;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JTextField holidayId;
    private javax.swing.JLabel holidayIdLabel;
    private javax.swing.JButton okButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private PublicHolidayDialog myParent;
}
